﻿using Library;
using StructureStudyApplication;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Library
{
    public class LibrarySystemNoLocalData : BaseLibrarySystem
    {
        protected override void AddBook(string title, string author, int quantity)
        {
            if (Books.TryGetValue(title, out var book)) book.StockNum += quantity;
            else Books[title] = new Book(title, author, quantity);
        }

        protected override void DeleBook(string title)
        {
            if (Books.TryGetValue(title, out var book))
            {
                Books.Remove(title);
                Console.WriteLine($"已删除书籍: 《{title}》");
            }
            else Console.WriteLine($"未找到书籍: 《{title}》!");
        }

        protected override bool BorrowBook(string title, int quantity)
        {
            if (Books.TryGetValue(title, out var book)) return book.Borrow(quantity);
            else
            {
                Console.WriteLine($"未找到书籍: 《{title}》!");
                return false;
            }
        }

        // 需做模糊查询 待处理
        protected override void QueryBook(string? title = null, string? author = null)
        {
            bool flag = false;
            foreach (var book in Books.Values)
            {
                if (title != null && book.Title.IndexOf(title) >= 0 ||
                    author != null && book.Author.IndexOf(author) >= 0)
                {
                    Console.WriteLine(book);
                    flag = true;
                }
            }
            if (!flag) Console.WriteLine($"未找到书籍: 《{title}》!");
        }

        protected override bool ReturnBook(string title, int quantity)
        {
            if (Books.TryGetValue(title, out var book)) return book.Return(quantity);
            else
            {
                Console.WriteLine($"未找到书籍: 《{title}》!");
                return false;
            }
        }

        public override void DisplayInventory()
        {
            if (Books.Count == 0) Console.WriteLine("暂未收录书籍!");
            else
            {
                foreach (var book in Books.Values)
                {
                    Console.WriteLine(book);
                }
            }
        }

        // 需做分页 待处理
        protected override void QueryBorrowedBook(User user)
        {
            user.DisplayBorrowedBooks();
        }
    }
}
